<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if (!defined('DS')) define('DS', DIRECTORY_SEPARATOR);

class Articles extends OC_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('articles/article_model','am');
		$this->load->model('articles/category_model','cm');
		$this->upload_path = OC_FRONT_DIR . 'ocimages' . DS . 'articles' . DS;
	}

	function _remap($method, $params = array())
	{
		parent::_remap($method, $params);
		
		if (method_exists($this, $method))
			return call_user_func_array(array($this, $method), $params);
		else
			show_404();
	}

	function init_header()
	{
		$this->header['jeasyui'] = TRUE;
		$this->header['categories'] = $this->cm->get_all()->rows;
		$this->header['source_bot'] = array(
			script_tag(site_url('articles/themescript/moment.js')),
			script_tag(site_url('articles/themescript/app/articles.js'))
		);
	}

	function index()
	{
		$this->init_header();
		$this->header['nav'] = 'articles';
		$this->header['pagetitle'] = 'Article';
		$this->view('articles/article_view');
	}

	function load_data()
	{
		$params = $this->input->post();
		$result = $this->am->get_all($params);
		print(json_encode($result));
	}

	function save_data()
	{
		$post = $this->input->post();
		if (isset($_FILES['userfile']) && !empty($_FILES['userfile']['name'])) {
			$this->load->library('upload', array(
				'upload_path' => $this->upload_path,
				'allowed_types' => 'jpg|gif|png|bmp|JPG|GIF|PNG|BMP',
				'encrypt_name' => true
			));
			
			if ($this->upload->do_upload()) {
				$uploaded = $this->upload->data();
				$post['a_filename'] = $uploaded['file_name'];
				$post['a_origname'] = $uploaded['orig_name'];
			}

		}
		//unset($post['files']);

		if (empty($post['a_id'])) {
			$post['a_created_date'] = date('Y-m-d H:i:s');
			$post['a_author'] = $this->bitauth->fullname;
			unset($post['a_id']);
			$this->am->add($post);
		} else {
			$post['a_modified_date'] = date('Y-m-d H:i:s');
			$this->am->edit($post, array(
				'a_id' => $post['a_id']
			));
		}

		print(json_encode(array(
			'success' => TRUE
		)));
	}

	function delete_data()
	{
		$post = $this->input->post();
		$this->am->delete(array('a_id' => $post['a_id']));
		print(json_encode(array(
			'success' => TRUE
		)));
	}

}

/* End of file articles.php */
/* Location: .//D/noobsprojects/swanstwenty/dnetnorfintek/module_admin/articles/controllers/articles.php */